<?php
/**
 * Blog Single Posts Modules
 */


/**
 * webtreats_related_post() - outputs HTML for related posts by tag
 * 
 */
function webtreats_related_post() {
	global $post, $wpdb;
	$backup = $post;  // backup the current object
	$tags = wp_get_post_tags($post->ID);
	$tagIDs = array();
	if ($tags) {
	  $tagcount = count($tags);
	  for ($i = 0; $i < $tagcount; $i++) {
	    $tagIDs[$i] = $tags[$i]->term_id;
	  }
	  $args=array(
	    'tag__in' => $tagIDs,
	    'post__not_in' => array($post->ID),
	    'showposts'=>4,
	    'caller_get_posts'=>1
	  );
	  $my_query = new WP_Query($args);
	  if( $my_query->have_posts() ) { $related_post_found = true; ?>
<ul class="post_container">		
		<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
		<?php $post_id = get_the_ID(); ?> 
		<?php $post_title = stripslashes($post->post_title);
		      $post_date = $post->post_date;
		      $post_date = mysql2date('m-j-Y', $post_date, false);
		      $permalink = get_permalink($post->ID);
		      $meta_image = get_post_meta($post->ID, "post_image", true);
			
			if(!$meta_image){
				$meta_image = get_template_directory_uri() .'/styles/_shared/default_post_thumb.png';
			}else{

				if(preg_match_all('!.+\.(?:jpe?g|png|gif)!Ui',$meta_image,$matches)){
					$meta_image = webtreats_image_resize($img_height=90, $img_width=126, $meta_image);	
				}

				if( ( preg_match_all('#http://(www.vimeo|vimeo)\.com(/|/clip:)(\d+)(.*?)#i',$meta_image,$matches) ) || ( preg_match( '#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com/(.*?)#i', $meta_image, $matches ) ) ){
					$meta_image = WEBTREATS_SCRIPTS_FOLDER .'/thumb.php?src=' .get_template_directory_uri(). '/styles/_shared/default_post_thumb.png&amp;w=126&amp;h=90&amp;zc=1&amp;q=100';

				}
			} ?>
				<li class="related_post">			
				<a href="<?php echo $permalink; ?>" title="<?php echo $post_title; ?>">
				<img class="framed fade_hover" src="<?php echo $meta_image; ?>" width="126" height="90" alt="<?php echo $post_title; ?>"/><br />
				<?php the_title(); ?></a>
				</li>
	    <?php endwhile; ?>
</ul>	
	  <?php }
	}
	$post = $backup;  // copy it back
	//wp_reset_query(); // moved to end of function
	
	//show recent posts if no related found
	if(!$related_post_found){
		$posts = get_posts('numberposts=4&offset=0');
		if($posts){ ?>
<ul class="post_container">
			<?php foreach($posts as $post){
					$post_title = stripslashes($post->post_title);
					$post_date = $post->post_date;
					$post_date = mysql2date('m-j-Y', $post_date, false);
					$permalink = get_permalink($post->ID);
					$meta_image = get_post_meta($post->ID, "post_image", true);
					
					if(!$meta_image){
						$meta_image = get_template_directory_uri() .'/styles/_shared/default_post_thumb.png';
					}else{

						if(preg_match_all('!.+\.(?:jpe?g|png|gif)!Ui',$meta_image,$matches)){
							$meta_image = webtreats_image_resize($img_height=90, $img_width=126, $meta_image);
						}

						if( ( preg_match_all('#http://(www.vimeo|vimeo)\.com(/|/clip:)(\d+)(.*?)#i',$meta_image,$matches) ) || ( preg_match( '#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com/(.*?)#i', $meta_image, $matches ) ) ){
							$meta_image = get_template_directory_uri() .'/styles/_shared/default_movie_thumb.png';
						}
					} ?>

					<li class="recent_post">			
					<a href="<?php echo $permalink; ?>" title="<?php echo $post_title; ?>">
						<img class="framed fade_hover" src="<?php echo $meta_image; ?>" width="126" height="90" alt="<?php echo $post_title; ?>"/><br />
						<?php echo $post_title; ?></a>
						</li>
					<?php } ?>
</ul>	
			<?php }
	}
	wp_reset_query(); // to use the original query again
}

/**
 * webtreats_popular_post() - outputs HTML for most popular posts by comment count
 * 
 */
function webtreats_popular_post() {
	global $wpdb;
	$pop_posts = 4;
	$now = gmdate("Y-m-d H:i:s",time());
	$lastmonth = gmdate("Y-m-d H:i:s",gmmktime(date("H"), date("i"), date("s"), date("m")-12,date("d"),date("Y")));
	$popularposts = "SELECT ID, post_title, post_date, COUNT($wpdb->comments.comment_post_ID) AS 'stammy' FROM $wpdb->posts, $wpdb->comments WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish' AND post_date < '$now' AND post_date > '$lastmonth' AND comment_status = 'open' GROUP BY $wpdb->comments.comment_post_ID ORDER BY stammy DESC LIMIT ".$pop_posts;
	
	$posts = $wpdb->get_results($popularposts);
	$posts = ($posts) ? $posts : get_posts('numberposts=4&offset=0');
	
	if($posts){ ?>
<ul class="post_container">		
		<?php foreach($posts as $post){ 
				$post_title = stripslashes($post->post_title);
				$post_date = $post->post_date;
				$post_date = mysql2date('m-j-Y', $post_date, false);
				$permalink = get_permalink($post->ID);
				$meta_image = get_post_meta($post->ID, "post_image", true);
				
				if(!$meta_image){
					$meta_image = get_template_directory_uri() .'/styles/_shared/default_post_thumb.png';
				}else{

					if(preg_match_all('!.+\.(?:jpe?g|png|gif)!Ui',$meta_image,$matches)){
						$meta_image = webtreats_image_resize($img_height=90, $img_width=126, $meta_image);	
					}

					if( ( preg_match_all('#http://(www.vimeo|vimeo)\.com(/|/clip:)(\d+)(.*?)#i',$meta_image,$matches) ) || ( preg_match( '#http://(www.youtube|youtube|[A-Za-z]{2}.youtube)\.com/(.*?)#i', $meta_image, $matches ) ) ){
						$meta_image = get_template_directory_uri() .'/styles/_shared/default_movie_thumb.png';
					}
				} ?>
					<li class="popular_post">
					<a href="<?php echo $permalink; ?>" title="<?php echo $post_title; ?>">
					<img class="framed fade_hover" src="<?php echo $meta_image; ?>" width="126" height="90" alt="<?php echo $post_title; ?>"/><br />
					<?php echo $post_title; ?></a>
					</li>
		<?php } ?>
</ul>
<?php }		
}

/**
 * webtreats_author_info() - outputs HTML for about the author box
 * 
 */
function webtreats_author_info() { ?>
<div class="fancy_titled_box" id="about_the_author">
	<h3 class="fancy_titled_box_header">About the Author</h3>
	<div class="fancy_titled_box_content">
		<?php //echo get_avatar( get_the_author_email(), '80' ); ?>
		<?php echo get_avatar(get_the_author_email(),$size='60',$default=get_template_directory_uri().'/styles/_shared/gravatar.gif' ); ?>
		<p class="padding"><strong><?php the_author_link(); ?></strong><br />
		<?php the_author_description(); ?></p>
		<div class="clearboth"></div>
	</div>	
</div>
<?php 
}
?>